package cn.chenmanman.system.controller;

import cn.chenmanman.common.result.Result;
import cn.chenmanman.model.system.SysMenu;
import cn.chenmanman.model.vo.AssginMenuVo;
import cn.chenmanman.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api( tags = "菜单管理" )
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 为角色分配菜单
     * @author 陈慢慢
     * */
    @ApiOperation("为角色分配菜单")
    @PostMapping("/assginMenu")
    public Result assginMenu(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.assginMenu(assginMenuVo);
        return Result.ok();
    }


    /**
     * 根据角色获取菜单
     * @author 陈慢慢
     * */
    @ApiOperation("根据角色获取菜单")
    @GetMapping("/byRoleId")
    public Result getMenuByRoleId(String roleId) {
        List<SysMenu> menuList = sysMenuService.getMenuByRoleId(roleId);
        return Result.ok(menuList);
    }

    /**
     * 菜单列表（树形）
     * @author 陈慢慢
     * @date 2022-11-19 21:20
     * */
    @ApiOperation("菜单列表(树形结构)")
    @GetMapping("/list")
    public Result findNodes() {
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        return Result.ok(sysMenuList);
    }

    /**
     * 根据id获得菜单
     * @author 陈慢慢
     * */
    @ApiOperation("根据id得到对应菜单")
    @GetMapping("/{menuId}")
    public Result getNode(@PathVariable String menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return  Result.ok(sysMenu);
    }

    /**
     * 添加新的菜单
     * @author 陈慢慢
     * */
    @ApiOperation("添加菜单")
    @PostMapping
    public Result addMenu(@RequestBody SysMenu sysMenu) {
        boolean menuSave = sysMenuService.save(sysMenu);
        return menuSave ? Result.ok(): Result.fail();
    }

    /**
     * 修改菜单
     * @author 陈慢慢
     * */
    @ApiOperation("修改菜单")
    @PutMapping
    public Result updateMenu(@RequestBody SysMenu sysMenu) {
        boolean menuSave = sysMenuService.updateById(sysMenu);
        return menuSave ? Result.ok(): Result.fail();
    }

    /**
     * 批量删除菜单
     * @author 陈慢慢
     * */
    @ApiOperation("批量删除菜单")
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        boolean removeByIds = sysMenuService.removeByIds(ids);
        return removeByIds ? Result.ok(): Result.fail();
    }

    @ApiOperation("删除指定菜单")
    @DeleteMapping("/{menuId}")
    public Result delete(@PathVariable Long menuId) {
        boolean removeById = sysMenuService.removeById(menuId);
        return removeById ? Result.ok(): Result.fail();
    }

}
